package com.jxbeidou.system.setting.controller;

import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.entity.R;
import com.jxbeidou.system.setting.convert.ParamsConvert;
import com.jxbeidou.system.setting.entity.ParamsDO;
import com.jxbeidou.system.setting.service.ParamsService;
import com.jxbeidou.system.setting.query.ParamsQuery;
import com.jxbeidou.system.setting.vo.ParamsVO;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
* 参数管理表
*
* @author 杜晓祥 
* @since 1.0.0 2023-12-14
*/
@RestController
@RequestMapping("/sys/params")
@AllArgsConstructor
public class ParamsController {
    @Resource
    private ParamsService paramsService;

    @PostMapping("/page")
    public R<PageResult<ParamsVO>> page(@RequestBody @Valid ParamsQuery query){
        PageResult<ParamsVO> page = paramsService.page(query);

        return R.success(page);
    }

    @GetMapping("/{id}")
    public R<ParamsVO> get(@PathVariable("id") Long id){
        ParamsDO entity = paramsService.getById(id);

        return R.success(ParamsConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    public R<String> save(@RequestBody ParamsVO vo){
        paramsService.save(vo);

        return R.saveSuccess();
    }

    @PutMapping
    public R<String> update(@RequestBody @Valid ParamsVO vo){
        paramsService.update(vo);

        return R.modifySuccess();
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable("id") Long id){
        paramsService.delete(Arrays.asList(id));

        return R.deleteSuccess();
    }

    @PutMapping("/delete")
    public R<String> delete(@RequestBody List<Long> idList){
        paramsService.delete(idList);

        return R.deleteSuccess();
    }
}